package com.hnichr.ump.assessment.controller;


import com.hnichr.ump.common.assessment.entity.TenantUser;
import com.hnichr.ump.common.assessment.service.TenantUserService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.security.entity.User;
import com.mxpioframework.security.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/assessment/tenantuser/v1/")
@Tag(name = "TenantUserController", description = "租户用户controller")
public class TenantUserController {

    @Autowired
    private TenantUserService tenantUserService;
    @Autowired
    private UserService userService;

    @GetMapping("list")
    @Operation(summary = "租户用户列表", description = "获取租户用户列表（无分页）", method = "GET")
    public Result<List<TenantUser>> list(@Parameter(description="查询参数") Criteria criteria){
        List<TenantUser> list = tenantUserService.list(TenantUser.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "租户用户列表", description = "租户用户列表（分页）", method = "GET")
    public Result<Page<TenantUser>> page(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<TenantUser> list = tenantUserService.listPage(TenantUser.class,page, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取租户用户", description = "根据ID获取租户用户", method = "GET")
    public Result<TenantUser> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        TenantUser tenantUser = tenantUserService.getById(TenantUser.class, id);
        return Result.OK(tenantUser);
    }

    @PostMapping("add")
    @Operation(summary = "新增租户用户", description = "新增租户用户", method = "POST")
    public Result<TenantUser> add(@RequestBody TenantUser tenantUser) {
        User user = userService.findByName(tenantUser.getUserName());
        if(user==null){
            return Result.error("用户输入错误");
        }
        tenantUser.setNickName(user.getNickname());
        return Result.OK(tenantUserService.save(tenantUser));
    }

    @PutMapping("edit")
    @Operation(summary = "编辑租户用户", description = "编辑租户用户", method = "PUT")
    public Result<TenantUser> edit(@RequestBody TenantUser tenantUser) {
        User user = userService.findByName(tenantUser.getUserName());
        if(user==null){
            return Result.error("用户输入错误");
        }
        tenantUser.setNickName(user.getNickname());
        return Result.OK(tenantUserService.update(tenantUser));
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除租户用户", description = "根据id删除租户用户", method = "DELETE")
    public Result<TenantUser> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            tenantUserService.remove(TenantUser.class, key);
        }
        return Result.OK();
    }
}
